from jinja2 import Environment,FileSystemLoader
import os
from PIL import Image
from getpic import GetPic
# if __name__=='__main__':
#     path="test.png"
#     data=[85,156,179.5,211,123]
#     labels=[u"星期一",u"星期二",u"星期三",u"星期四",u"星期五"]
#
#     chart=XYChart(500,500)
#     chart.setPlotArea(50,50,400,400)
#
#     chart.xAxis().setTitle("时间","simsun.ttc",10)
#     chart.yAxis().setTitle("流量（MB）", "simsun.ttc", 10)
#     chart.setDefaultFonts("simsun.ttc")
#
#     chart.xAxis().setLabels(labels)
#     color=[0xFF7979,0xA5D770,0x3DAAED,0xFF9F3F,0x4BCA7E]
#     chart.addBarLayer3(data,color)
#
#     chart.makeChart(path)

# #将给定路径的图片转化为字节数据
# def get_picbase64(path):
#     file=open(path,'rb')
#     data=base64.b64encode(file.read())
#     file.close()
#     return data

def export_word_one_ip(data):
    getpic=GetPic()
    pic_path=getpic.get_pic_one(data)
    env=Environment(Loader=FileSystemLoader('/test/template'))
    template=env.get_template('xml_tempate_one_ip.xml')

    file_name=data['file_name']
    word_path=os.path.join("/test/doc",file_name.encode("utf-8"))
    word_content=open(word_path.encode("utf-8"),'w')

    getpic=GetPic()
    pic_path=getpic.get_pic_one(data)
    data["w_id"]='_Toc3557027500'
    data["w_name"]='wordml://03000001.png'
    data["pic_path"]=pic_path

    word_content.write(template.render(tableInfo=data))
    word_content.close()





def crop(path):
    image = Image.open(path)
    xsize, ysize = image.size
    box = (1, 1, xsize - 2, ysize - 9)
    image.crop(box).save(path)





import base64
from docx import Document
from docx.shared import Inches

# # 读取Base64图像数据
# with open("image_base64.txt", "r") as file:
#     image_data = file.read()
crop('test.png')  #去掉黄条和水印
getpic=GetPic()
image_data=getpic.get_picbase64('test.png')

# 创建Word文档
doc = Document()

# 添加一个段落
paragraph = doc.add_paragraph()

# 将Base64图像数据写入临时文件
with open("temp_image.png", "wb") as file:
    file.write(base64.b64decode(image_data))

# 插入图片
paragraph.add_run().add_picture("temp_image.png", width=Inches(2))

# 删除临时文件
import os
os.remove("temp_image.png")

# 保存Word文档
doc.save("output.docx")